package cn.alex.sso.project.service.impl;

import cn.alex.sso.framework.core.PageHelper;
import cn.alex.sso.framework.enums.StatusEnum;
import cn.alex.sso.project.entity.SysClient;
import cn.alex.sso.project.entity.params.EditSysClientStatus;
import cn.alex.sso.project.mapper.SysClientMapper;
import cn.alex.sso.project.mapper.SysUserRoleMapper;
import cn.alex.sso.project.service.SysClientAllowService;
import cn.alex.sso.project.service.SysClientService;
import cn.alex.sso.project.service.SysClientSyncService;
import cn.alex.sso.project.service.SysRoleClientService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Alex
 * @description 针对表【sys_client(应用表)】的数据库操作Service实现
 * @createDate 2024-09-24 18:59:26
 */
@Service
@RequiredArgsConstructor
public class SysClientServiceImpl extends ServiceImpl<SysClientMapper, SysClient> implements SysClientService {
    @Lazy
    @Autowired
    private SysClientAllowService clientAllowService;
    private final SysClientSyncService clientSyncService;
    private final SysRoleClientService roleClientService;
    private final SysUserRoleMapper userRoleMapper;

    @Override
    public SysClient getLcSsoServerClient() {
        return baseMapper.selectOne(new LambdaQueryWrapper<SysClient>()
                .eq(SysClient::getIsLcSsoServer, 1)
        );
    }

    @Override
    public IPage<SysClient> getPage(SysClient entity) {
        return baseMapper.selectPage(PageHelper.getPage(), baseMapper.getQueryWrapper(entity));
    }

    @Override
    public void add(SysClient entity) {
        entity.setCreatedTime(new Date());
        entity.setUpdatedTime(new Date());
        entity.setSecretKey(IdUtil.fastSimpleUUID());
        baseMapper.insert(entity);
    }

    @Override
    public void edit(SysClient entity) {
        entity.setUpdatedTime(new Date());
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        baseMapper.deleteByIds(ids);
        ids.forEach(id -> {
            clientAllowService.removeByClientId(id);
            clientSyncService.removeByClientId(id);
            roleClientService.removeByClientId(id);
        });

    }

    @Override
    public List<SysClient> getAllowListByUserId(Long userId) {
        List<Long> roleIdList = userRoleMapper.getRoleIdListByUserId(userId);
        if (CollUtil.isEmpty(roleIdList)) {
            return Collections.emptyList();
        }

        List<Long> clientIdList = roleClientService.getClientIdListByRoleId(roleIdList);
        if (CollUtil.isEmpty(clientIdList)) {
            return Collections.emptyList();
        }

        return baseMapper.selectList(new LambdaQueryWrapper<SysClient>()
                .in(SysClient::getId, clientIdList)
                .eq(SysClient::getStatus, StatusEnum.YES.getStatus())
        );
    }

    @Override
    public void editStatus(EditSysClientStatus editSysClientStatus) {
        baseMapper.update(new LambdaUpdateWrapper<SysClient>()
                .eq(SysClient::getId, editSysClientStatus.getId())
                .set(ObjUtil.isNotEmpty(editSysClientStatus.getIsSso()), SysClient::getIsSso, editSysClientStatus.getIsSso())
                .set(ObjUtil.isNotEmpty(editSysClientStatus.getIsSync()), SysClient::getIsSync, editSysClientStatus.getIsSync())
                .set(ObjUtil.isNotEmpty(editSysClientStatus.getStatus()), SysClient::getStatus, editSysClientStatus.getStatus())
        );
    }
}




